﻿using AutoMapper;
using MediatR;
using Traceable.App.Application.Command.Aquaculture;
using Traceable.Domian;
using Traceable.ErrorCode;
using Traceable.Infastructure;

namespace Traceable.App.Application.Handler.Aquaculture
{
    public class UpImageListHandler:IRequestHandler<UpImageListCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<Image_Table> _imageRepository;
        private readonly IMapper _mapper;

        public UpImageListHandler(IBaseRepository<Image_Table> imageRepository, IMapper mapper)
        {
            _imageRepository = imageRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 保存图片到数据库
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<int>> Handle(UpImageListCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            var model =_mapper.Map<Image_Table>(request);
            var res =_imageRepository.Add(model);
            if(res>0)
            {
                result.Code = StatusEnum.OK;
                result.Data = res;
                result.Message = "添加成功";
            }
            else
            {
                result.Code = StatusEnum.Error;
                result.Data = res;
                result.Message = "添加失败";
            }
            return Task.FromResult(result);
        }
    }
}
